/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools;

import embeddedpaint.EmbeddedPaint;
import embeddedpaint.model.feature.AImageTool;
import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerImage;
import embeddedpaint.model.layers.LayerManager;
import embeddedpaint.user_interface.tools.dialog.FrameScalePicture;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;


public class ToolsScalePicture extends AImageTool implements MouseMotionListener, MouseListener, Observer
{
    private static final String M_LABEL_TEXT                              = "Scale Layer";
    private static final String M_ICON_PATH                               = "images/scale.png";
    private static final String M_ERROR_MESSAGE_TEXT_LAYER_SELECTED_TEXT  = "Sorry, but you can't scale a text layer.\nPlease remove selected text Layer.";
    private static final String M_ERROR_MESSAGE_TEXT_LAYER_SELECTED_TITLE = "Scale forbiden";
    
    private FrameScalePicture   m_frame;
    private Point               m_lastPoint;

    public ToolsScalePicture (LayerManager imageManager, JFrame application)
    {
        super(imageManager, application);
    }

    @Override
    public boolean isToggleButton ()
    {
        return true;
    }

    @Override
    public String getLabel ()
    {
        return M_LABEL_TEXT;
    }

    @Override
    protected String getIconName ()
    {
        return M_ICON_PATH;
    }

    private boolean checkIfSelectionIsScalable ()
    {
        if (!this.m_imageManager.selectedLayerIsOnlyLayerImage())
        {
            JOptionPane.showMessageDialog(m_application, M_ERROR_MESSAGE_TEXT_LAYER_SELECTED_TEXT, M_ERROR_MESSAGE_TEXT_LAYER_SELECTED_TITLE, JOptionPane.ERROR_MESSAGE);
            
            return false;
        }
        
        return true;
    }

    @Override
    public void actionPerformed (ActionEvent e)
    {
        if (checkIfSelectionIsScalable())
        {
            if (e.getSource() instanceof JToggleButton)
            {
                JToggleButton buton = (JToggleButton) e.getSource();

                if (buton.isSelected())
                {
                    createObserver();
                }
                else
                {
                    removeObservers();
                }
            }
        }
        else
        {
            this.setChanged();
            this.notifyObservers();
        }
    }

    public void createObserver ()
    {
        m_frame = new FrameScalePicture(m_imageManager, m_application);
        
        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseMotionListener(this);
        
        m_frame.addObserver(this);
        
        this.m_actif = true;
    }

    public void removeObservers ()
    {
        this.m_actif = false;
        
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseMotionListener(this);
        
        if (m_frame != null)
        {
            m_frame = null;
        }
        
        this.setChanged();
        this.notifyObservers();
    }

    @Override
    public void dispose ()
    {
        if (m_frame != null)
        {
            m_frame.removeObserver(this);
            m_frame.dispose();
        }
        removeObservers();
    }

    @Override
    public void mouseClicked (MouseEvent e)
    {
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        m_lastPoint = e.getPoint();
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
    }

    @Override
    public void mouseEntered (MouseEvent e)
    {
    }

    @Override
    public void mouseExited (MouseEvent e)
    {
    }

    @Override
    public void mouseDragged (MouseEvent e)
    {
        Point newPoint = e.getPoint();
        int   offsetX  = newPoint.x - m_lastPoint.x;
        int   offsetY  = newPoint.y - m_lastPoint.y;
        
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            if (img instanceof LayerImage)
            {
                LayerImage imageLayer = (LayerImage) img;
                imageLayer.setScale(offsetX + img.getM_width(), offsetY + img.getM_height());
            }
        }
        
        this.m_lastPoint = newPoint;
    }

    @Override
    public void mouseMoved (MouseEvent e)
    {
    }

    @Override
    public void update (Observable o, Object arg)
    {
        this.removeObservers();
    }
}
